package com.xiaobai.zhuangxiuguanjia.adapters;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.xiaobai.zhuangxiuguanjia.R;
import com.xiaobai.zhuangxiuguanjia.Utils.RequestManager;
import com.xiaobai.zhuangxiuguanjia.Utils.ViewHolder;
import com.xiaobai.zhuangxiuguanjia.model.AppData;

import java.util.List;

/**
 * Created by Android Studio
 * Project: ZhuangXiuGuanJia
 * Author: Jack
 * Email: jackfangqi1314@gmail.com/jackfangqi@163.com
 * Date: 2015/6/17
 */
public class RecommendAppListAdapter extends BaseAdapter {

    private List<AppData> appDataList;
    private Context context;
    private ImageLoader imageLoader;

    public RecommendAppListAdapter(Context context, List<AppData> appDataList) {
        this.context = context;
        this.appDataList = appDataList;

        if (context != null) {
            imageLoader = RequestManager.getInstance(context).getImageLoader();
        }
    }

    @Override
    public int getCount() {
        if (appDataList != null) {
            Log.d("size", String.valueOf(appDataList.size()));
            return appDataList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (appDataList != null) {
            return appDataList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null)
            convertView = LayoutInflater.from(context).inflate(R.layout.app_item_layout, parent, false);

        ImageView appIcon = ViewHolder.getView(convertView, R.id.app_icon);
        TextView appName = ViewHolder.getView(convertView, R.id.app_name);
        TextView appIntro = ViewHolder.getView(convertView, R.id.app_intro);

        AppData appData = appDataList.get(position);

        String iconUrl = appData.getIcon();
        imageLoader.get(iconUrl, ImageLoader.getImageListener(appIcon, R.mipmap.ic_launcher, R.mipmap.ic_launcher));
        appName.setText(appData.getAppname());
        appIntro.setText(appData.getAppdesc());

        return convertView;
    }
}
